import matplotlib.pyplot as plt
import time

def plot_sensor_data(raw_data):
    """绘制传感器数据可视化图表"""
    if not raw_data:
        return

    # 提取各轴数据
    acc_x = [sample[0] for sample in raw_data]
    acc_y = [sample[1] for sample in raw_data]
    acc_z = [sample[2] for sample in raw_data]
    gyro_x = [sample[3] for sample in raw_data]
    gyro_y = [sample[4] for sample in raw_data]
    gyro_z = [sample[5] for sample in raw_data]

    plt.figure(figsize=(12, 8))

    # 绘制加速度数据
    plt.subplot(2, 1, 1)
    plt.plot(acc_x, label='X轴')
    plt.plot(acc_y, label='Y轴')
    plt.plot(acc_z, label='Z轴')
    plt.title('加速度数据')
    plt.ylabel('加速度 (g)')
    plt.legend()
    plt.grid(True)

    # 绘制角速度数据
    plt.subplot(2, 1, 2)
    plt.plot(gyro_x, label='X轴')
    plt.plot(gyro_y, label='Y轴')
    plt.plot(gyro_z, label='Z轴')
    plt.title('角速度数据')
    plt.xlabel('样本')
    plt.ylabel('角速度 (°/s)')
    plt.legend()
    plt.grid(True)

    plt.tight_layout()

    # 保存图表
    timestamp = time.strftime("%Y%m%d_%H%M%S")
    plt.savefig(f'./plotdata/sensor_data_{timestamp}.png')
    print(f"传感器数据图表已保存为 sensor_data_{timestamp}.png")

    # 显示图表
    plt.show()